﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using Microsoft.Practices.Unity;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class PagoRepository:MasterDataAccess, IPagoRepository
    {
        public List<DETALLEPAGO> GetAllFromPagos()
        {
            var query = from p in Context.DETALLEPAGOes.Include("CREDITO")
                        orderby p.Fecha descending
                        select p;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }
        
        public List<DETALLEPAGO> GetFromPagosbyCreditoId(int creditoId)
        {
            var query = from c in Context.DETALLEPAGOes
                    where c.CreditoId.Equals(creditoId)
                    select c;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public DETALLEPAGO GetFromPagoById(int id)
        {
            var query = from p in Context.DETALLEPAGOes
                        where p.Id.Equals(id)
                        select p;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddPago(DETALLEPAGO pago)
        {
            Context.DETALLEPAGOes.Add(pago);            
            Context.SaveChanges();          
        }

        public void UpdatePago(DETALLEPAGO pago)
        {
            Context.Entry(pago).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void CancelarPago(int id)
        {
            var existe = Context.DETALLEPAGOes.Find(id);
            Context.DETALLEPAGOes.Remove(existe);
            Context.SaveChanges();
        }


        
    }
}
